<?php


namespace App\Services\Game;

use App\DTOS\Admin\ServerGroupDTO;
use App\Exceptions\ServiceException;
use App\Models\Game\ClientVersion;
use App\Models\Game\GameChannel;
use App\Models\Game\GamePackage;
use App\Models\Game\GroupChannel;
use App\Models\Game\GroupPackage;
use App\Models\Game\ServerGroup;


use App\Models\Game\HotVersion;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use voku\helper\ASCII;

class ClientVersionService
{
    public function paginate(ServerGroupDTO $DTO){

        $model = ClientVersion::query();
        $pageSize = $DTO->getLimit();
        $page = $DTO->getPage();
        $name = $DTO->getName();
        if($name) {
            $model->where('name','like',"%{$name}%");
        }

        return $model->orderByDesc('id')->paginate($pageSize,'*','page',$page);
    }
    public function newpaginate(ServerGroupDTO $DTO){


        $model = ServerGroup::query();

        $hvmodel = ClientVersion::query()->get();
        $pno = $hvmodel->pluck('package_no')->toArray();
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);

        //从模型中获取对应字段值
        MyLog::info('id'.json_encode($pnos));
        $pageSize = $DTO->getLimit();
        $page = $DTO->getPage();
        $name = $DTO->getName();
        if($name) {
            $model->where('name','like',"%{$name}%");
        }if($pnos){
            $model->whereNotIn('id',$pnosArray);
        }
        $re = $model->whereNotIn('id',$pnosArray)->get();

        MyLog::info('$model结果'.json_encode($re));

        return $model->orderByDesc('id')->paginate($pageSize,'*','page',$page);
    }

    public function delete($id){
        DB::beginTransaction();
        try {
            $hotvs = ClientVersion::query()->where('id', $id)->first();
            $pno = [$hotvs->package_no];
            $pnos = implode(',', $pno);
            $pnosArray = explode(',', $pnos);
            ClientVersion::query()->where('id', $id)->delete();
            foreach ($pnosArray as $ids) {
                GamePackage::where('id', $ids)
                    ->update(['hot_version' => null]);
            }

            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
        return true;
    }

    /**
     * @param $Id
     * @param $data
     * @throws ServiceException
     */
    public function edit($Id,$data){
        $packno = data_get($data,'package_no');
        $name = data_get($data,['name']);
        $filename= data_get($data,['filename']);
        $hotversion= data_get($data,['hot_version']);
        $channelIds = data_get($data,'channelIds');
        $packageIds = data_get($data,'packageIds');
        //从新传入的包号中剔除原来的已经存在进行判断

        $packnoArray = explode(',', $packno);

        $filteredPackageIds = array_diff($packageIds, $packnoArray);

        //获取packagename值
        $packageName = data_get($data,'packages', []);
        //将数组转换成逗号隔开的字符串
        $packagenNames = implode(',', array_column($packageName, 'package_name'));
        $packageIdsString = implode(',', $packageIds);
        $channelIdsString = implode(',', $channelIds);


        MyLog::info('$data查看全部传入参数'.json_encode($data));
        try {
        //查询$packageIds是否在其他分组数据
        $items=[];
        foreach ($filteredPackageIds as $ids){
            $item=ClientVersion::query()->where('package_no', 'LIKE', "%$ids%")->get()->toArray();
            MyLog::info('$items'.json_encode($item));
            $items = array_merge($items, $item);
        }
            if(count($items)>1 ){
                throw new ServiceException("数据存在重复,请重新选择或者删除分组");
            }
        $hotvs= ClientVersion::query()->where('id',$Id)->first();
        $pno = [$hotvs->package_no];
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);


        DB::beginTransaction();

            //查询该分组之前在把之前分组的在gamepackage里面对应的值清空
            foreach ($pnosArray as $pns){
                GamePackage::where('id', $pns)
                    ->update(['hot_version' => null]);
            }
            ClientVersion::query()->where('id',$Id)->update(['hot_version' => $hotversion,'name' => $name,'filename' => $filename,'package_name'=>$packagenNames,'package_no'=>$packageIdsString,'channel_id'=>$channelIdsString]);
            //往版本号表插入
            foreach ($packageIds as $packageId){
                GamePackage::where('id', $packageId)
                    ->update(['hot_version' => $hotversion]);
            }

            DB::commit();
        } catch (\Exception $e){
            DB::rollBack();
            throw new ServiceException($e->getMessage());
        }

    }
        public function add($data){
            $name = data_get($data,['name']);
            $filename = data_get($data,['filename']);
            $hotversion= data_get($data,'hot_version');
            $packageIds = data_get($data,'packageIds');
            $channelIds = data_get($data,'channelIds');
            $packageNo = data_get($data,'packages', []);
            //将数组转换成逗号隔开的字符串
            $packagenNos = implode(',', array_column($packageNo, 'package_name'));

        DB::beginTransaction();

        try {
            $packageIdsString = implode(',', $packageIds);
            $channelIdsString = implode(',', $channelIds);
            $datarow=[];
                $datarow['package_no']=$packageIdsString;
                $datarow['package_name']=$packagenNos;
                $datarow['channel_id']=$channelIdsString;
                $datarow['hot_version']=$hotversion;
                $datarow['name']=$name;
                $datarow['filename']=$filename;
                $datarow['created_at']=now();
            ClientVersion::query()->insert($datarow);

            //往版本号表插入
            foreach ($packageIds as $packageId){
                MyLog::info('$packageId值'.json_encode($packageId));

                GamePackage::where('id', $packageId)
                    ->update(['hot_version' => $hotversion]);
            }
            DB::commit();

        } catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }
    public function detail($id){


        $hotvs= ClientVersion::query()->where('id',$id)->first();
        MyLog::info('$hotvs'.json_encode($hotvs));
        $pno = [$hotvs->package_no];
        $pnos = implode(',', $pno);
        $pnosArray = explode(',', $pnos);
        $cno = [$hotvs->channel_id];
        $cnos = implode(',', $cno);
        $cnosArray = explode(',', $cnos);

        //根据包号查询包号基本信息
        $gamepackage = GamePackage::query()->whereIn('id',$pnosArray)->get();
        $gameChannel = GameChannel::query()->whereIn('id',$cnosArray)->get();

        MyLog::info('$gamepackage'.json_encode($gamepackage));

        $hotvs['packages']=$gamepackage;
        $hotvs['channels']=$gameChannel;
        MyLog::info('$hotvs'.json_encode($hotvs));

        return $hotvs;
    }


}
